<template>
  <el-dialog
    :title="!dataForm.operId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="模块标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="模块标题"></el-input>
    </el-form-item>
    <el-form-item label="业务类型（0其它 1新增 2修改 3删除）" prop="businessType">
      <el-input v-model="dataForm.businessType" placeholder="业务类型（0其它 1新增 2修改 3删除）"></el-input>
    </el-form-item>
    <el-form-item label="方法名称" prop="method">
      <el-input v-model="dataForm.method" placeholder="方法名称"></el-input>
    </el-form-item>
    <el-form-item label="请求方式" prop="requestMethod">
      <el-input v-model="dataForm.requestMethod" placeholder="请求方式"></el-input>
    </el-form-item>
    <el-form-item label="操作类别（0其它 1后台用户 2手机端用户）" prop="operatorType">
      <el-input v-model="dataForm.operatorType" placeholder="操作类别（0其它 1后台用户 2手机端用户）"></el-input>
    </el-form-item>
    <el-form-item label="操作人员" prop="operName">
      <el-input v-model="dataForm.operName" placeholder="操作人员"></el-input>
    </el-form-item>
    <el-form-item label="部门名称" prop="deptName">
      <el-input v-model="dataForm.deptName" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="请求URL" prop="operUrl">
      <el-input v-model="dataForm.operUrl" placeholder="请求URL"></el-input>
    </el-form-item>
    <el-form-item label="主机地址" prop="operIp">
      <el-input v-model="dataForm.operIp" placeholder="主机地址"></el-input>
    </el-form-item>
    <el-form-item label="操作地点" prop="operLocation">
      <el-input v-model="dataForm.operLocation" placeholder="操作地点"></el-input>
    </el-form-item>
    <el-form-item label="请求参数" prop="operParam">
      <el-input v-model="dataForm.operParam" placeholder="请求参数"></el-input>
    </el-form-item>
    <el-form-item label="返回参数" prop="jsonResult">
      <el-input v-model="dataForm.jsonResult" placeholder="返回参数"></el-input>
    </el-form-item>
    <el-form-item label="操作状态（0正常 1异常）" prop="status">
      <el-input v-model="dataForm.status" placeholder="操作状态（0正常 1异常）"></el-input>
    </el-form-item>
    <el-form-item label="错误消息" prop="errorMsg">
      <el-input v-model="dataForm.errorMsg" placeholder="错误消息"></el-input>
    </el-form-item>
    <el-form-item label="操作时间" prop="operTime">
      <el-input v-model="dataForm.operTime" placeholder="操作时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          operId: 0,
          title: '',
          businessType: '',
          method: '',
          requestMethod: '',
          operatorType: '',
          operName: '',
          deptName: '',
          operUrl: '',
          operIp: '',
          operLocation: '',
          operParam: '',
          jsonResult: '',
          status: '',
          errorMsg: '',
          operTime: ''
        },
        dataRule: {
          title: [
            { required: true, message: '模块标题不能为空', trigger: 'blur' }
          ],
          businessType: [
            { required: true, message: '业务类型（0其它 1新增 2修改 3删除）不能为空', trigger: 'blur' }
          ],
          method: [
            { required: true, message: '方法名称不能为空', trigger: 'blur' }
          ],
          requestMethod: [
            { required: true, message: '请求方式不能为空', trigger: 'blur' }
          ],
          operatorType: [
            { required: true, message: '操作类别（0其它 1后台用户 2手机端用户）不能为空', trigger: 'blur' }
          ],
          operName: [
            { required: true, message: '操作人员不能为空', trigger: 'blur' }
          ],
          deptName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          operUrl: [
            { required: true, message: '请求URL不能为空', trigger: 'blur' }
          ],
          operIp: [
            { required: true, message: '主机地址不能为空', trigger: 'blur' }
          ],
          operLocation: [
            { required: true, message: '操作地点不能为空', trigger: 'blur' }
          ],
          operParam: [
            { required: true, message: '请求参数不能为空', trigger: 'blur' }
          ],
          jsonResult: [
            { required: true, message: '返回参数不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '操作状态（0正常 1异常）不能为空', trigger: 'blur' }
          ],
          errorMsg: [
            { required: true, message: '错误消息不能为空', trigger: 'blur' }
          ],
          operTime: [
            { required: true, message: '操作时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.operId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.operId) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysoperlog/info/${this.dataForm.operId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.sysOperLog.title
                this.dataForm.businessType = data.sysOperLog.businessType
                this.dataForm.method = data.sysOperLog.method
                this.dataForm.requestMethod = data.sysOperLog.requestMethod
                this.dataForm.operatorType = data.sysOperLog.operatorType
                this.dataForm.operName = data.sysOperLog.operName
                this.dataForm.deptName = data.sysOperLog.deptName
                this.dataForm.operUrl = data.sysOperLog.operUrl
                this.dataForm.operIp = data.sysOperLog.operIp
                this.dataForm.operLocation = data.sysOperLog.operLocation
                this.dataForm.operParam = data.sysOperLog.operParam
                this.dataForm.jsonResult = data.sysOperLog.jsonResult
                this.dataForm.status = data.sysOperLog.status
                this.dataForm.errorMsg = data.sysOperLog.errorMsg
                this.dataForm.operTime = data.sysOperLog.operTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysoperlog/${!this.dataForm.operId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'operId': this.dataForm.operId || undefined,
                'title': this.dataForm.title,
                'businessType': this.dataForm.businessType,
                'method': this.dataForm.method,
                'requestMethod': this.dataForm.requestMethod,
                'operatorType': this.dataForm.operatorType,
                'operName': this.dataForm.operName,
                'deptName': this.dataForm.deptName,
                'operUrl': this.dataForm.operUrl,
                'operIp': this.dataForm.operIp,
                'operLocation': this.dataForm.operLocation,
                'operParam': this.dataForm.operParam,
                'jsonResult': this.dataForm.jsonResult,
                'status': this.dataForm.status,
                'errorMsg': this.dataForm.errorMsg,
                'operTime': this.dataForm.operTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
